package myapp.Chat;

import java.util.ArrayList;

import staticClass.XmppConnection;

import myapp.DTO.UserDTO;
import myapp.EnumAndConstans.Constants;
import myapp.googlemap.R;
import myapp.services.ChatServiceHelper;
import android.app.Dialog;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class RequesterListDialog extends Dialog {
	private ListView lvRequesters = null;
	private XmppClient xmppClient;
	private ArrayList<UserDTO> requesters;

	public RequesterListDialog(XmppClient client) {
		super(client);
		// TODO Auto-generated constructor stub
		this.xmppClient = client;
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		setContentView(R.layout.chat_user_list_dialog);
		getWindow().setFlags(Constants.WINDOW_FLAG_FOR_DIALOG, Constants.WINDOW_MASKS_FOR_DIALOG);
		setTitle("Your Requests");

		InitializeListView();
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
	}

	private void InitializeListView() {
		Log.i("RequesterListDialog", "[InitializeListView]");
		setItemClickListener();
		setYourFriendsToListView();
	}

	private void setItemClickListener() {
		Log.i("RequesterListDialog", "[setItemClickListener]");

		lvRequesters = (ListView) findViewById(R.id.lvUsers);
		lvRequesters.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Log.i("FriendListDialog", "[onItemClick] position:" + position
						+ " id:" + id);
//				UserDTO requester = (UserDTO) parent
//						.getItemAtPosition(position);
//				AcceptRequest(xmppClient.getUserEmail(), requester.email);
//				ProcessAcceptButtonClicked(view);
			}
		});
	}
	
	private void setYourFriendsToListView() {
		Log.i("RequesterListDialog", "[setYourFriendsToListView] start");
		ChatServiceHelper helper = new ChatServiceHelper();
		if (requesters != null) {
			requesters.clear();
			requesters = null;
		}

		requesters = helper.getAllRequesters(XmppConnection.userEmail,
				XmppConnection.hashPwd);
		// ArrayAdapter<UserDTO> adapter = new
		// ArrayAdapter<UserDTO>(getContext(), R.layout.multi_line_list_item,
		// friends);
		RequesterAdapter adapter = new RequesterAdapter(getContext(),
				R.layout.chat_requester_info_item_list, requesters, XmppConnection.userEmail);
		lvRequesters.setAdapter(adapter);
		Log.i("RequesterListDialog", "[setYourFriendsToListView] done");
	}

//	private void ProcessAcceptButtonClicked(View view) {
//		try {
//			Button btnClicked = (Button) view
//					.findViewById(R.id.btnAcceptRequest);
//			btnClicked.setText("Accepted!");
//			btnClicked.setOnClickListener(null);
//			btnClicked.setClickable(false);
//		} catch (Exception e) {
//			// TODO: handle exception
//		}
//	}
}
